/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vn.support.ui.frame;

import java.util.ResourceBundle;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import vn.support.connection.Connection;
import vn.support.connection.RequestConnection;
import vn.support.connection.response.processor.impl.LoginProcessor;
import vn.support.core.dto.DataObject;
import vn.support.system.SystemProperties;
import vn.support.ui.View;
import vn.support.util.StringUtil;

/**
 * @author tuyen.nt
 */
public abstract class AbstractLoginView extends JFrame implements View {
    protected final Connection connection;
    protected ResourceBundle bundle;
    protected LoginProcessor processor = null;

    public AbstractLoginView () {
        super.setIconImage(new ImageIcon(getClass().getResource(SystemProperties.getValue("support.chat.system.icon"))).getImage());

        this.connection = new RequestConnection();
        this.bundle = ResourceBundle.getBundle(View.UI_BUNDLE);
        this.processor = null;
    }

    protected String bundle(String key) {
        if(this.bundle.containsKey(key)) {
            return this.bundle.getString(key);
        }
        return key;
    }

    protected void login(String username, String password) {
        this.deactive();
        if(this.processor != null) return;
        this.processor = new LoginProcessor(this);
        DataObject data = new DataObject(Connection.LOGIN_ACTION);
        data.set("username", username);
        data.set("password", StringUtil.md5(password));
        data.set(Connection.RESPONSE_PROCESSOR, this.processor);
        connection.execute(data);
    }

    public abstract void active();
    public abstract void deactive();
}
